package com.zzy.util;

import java.io.IOException;
import java.net.MalformedURLException;

import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

public class HtmlToFileUtil {


	public static String ConverHtmlToFile(String targerUrl,String htmlFilePath)throws FailingHttpStatusCodeException, MalformedURLException,IOException {
		// 模拟一个浏览器
		WebClient webClient = new WebClient();
		webClient.setAjaxController(new NicelyResynchronizingAjaxController());
		webClient.setJavaScriptEnabled(true);
		webClient.setThrowExceptionOnFailingStatusCode(false);
		webClient.setThrowExceptionOnScriptError(false);
		// 模拟浏览器打开一个目标网址
		HtmlPage rootPage = webClient.getPage(targerUrl);
		//获取页面文本
		String textStr=rootPage.asXml();
		System.out.println(textStr);
		
		//酱获取到的  html转成文件
		FileOperation.contentToTxt(htmlFilePath,textStr);
		
		return htmlFilePath;
	}
}